﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using FacebookApp;

public partial class Pages_Search : System.Web.UI.Page
{
    private static string GroupProductId = "";
    private static List<Ebook> dt = new List<Ebook>();
    private static System.Web.UI.WebControls.DataList current = new System.Web.UI.WebControls.DataList();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //List<Ebook> dt = new List<Ebook>();
            this.GetGroupProductId(Session["GroupProduct"].ToString());
            string a = GroupProductId.Substring(0, GroupProductId.Length - 1);
            GroupProductId = Session["GroupProduct"].ToString();
            if (Session["keyword"] == null)
            {
                if (Session["Price1"] != null && Session["Price2"] == null)
                {
                    dt = EbookService.Ebook_GetByTop("", "Price >= " + Session["Price1"] + " AND Category_Id IN ( " + a + ")", "Price DESC");
                }
                else
                {
                    if (Session["Price1"] == null && Session["Price2"] != null)
                    {
                        dt = EbookService.Ebook_GetByTop("", "Price <= " + Session["Price2"] + " AND Category_Id IN ( " + a + ")", "Price DESC");
                    }
                    else
                    {
                        if (Session["Price1"] != null && Session["Price2"] != null)
                        {
                            dt = EbookService.Ebook_GetByTop("", "Price >= " + Session["Price1"] + " AND Price <= " +
                                                                Session["Price2"] + " AND Category_Id IN ( " + a + ")", "Price DESC");
                        }
                        else
                        {
                            dt = EbookService.Ebook_GetByTop("", "Category_Id IN ( " + a + ")", "Price DESC");
                        }
                    }
                }
                //CollectionPager1.DataSource = dt.DefaultView;
                //CollectionPager1.BindToControl = dlNhomSanPham;
            }
            else
            {
                if (Session["Price1"] == null)
                {
                    Session["Price1"] = "0";
                }
                if (Session["Price2"] == null)
                {
                    Session["Price2"] = "10000000000";
                }
                dt = EbookService.Ebook_Search(Session["flag"].ToString(), Session["keyword"].ToString(), Session["Price1"].ToString(), Session["Price2"].ToString(), a);
               // WebMsgBox.Show(dt[0].Name);
                //String[] GetId = a.Split(',');
                //DataTable temp = null;
                //for (int i = 0; i < GetId.Length; i++)
                //{
                //    dt = ProductService.Product_Search(Session["flag"].ToString(), Session["keyword"].ToString(), Session["Price1"].ToString(), Session["Price2"].ToString(), GetId[i]);
                //    if (temp == null && dt.Rows.Count > 0)
                //    {
                //        temp = dt;
                //    }
                //    else
                //    {

                //        for (int j = 0; j < dt.Rows.Count; j++)
                //        {
                //            temp.Rows.Add(dt.Rows[j]);
                //        }
                //    }

                //}
                //dt = temp;
                //if (temp == null)
                //    return;
                //dt = ProductService.Product_Search(Session["flag"].ToString(), Session["keyword"].ToString(), Session["Price1"].ToString(), Session["Price2"].ToString(), a);
                //CollectionPager1.DataSource = temp.DefaultView;
                //CollectionPager1.BindToControl = dlNhomSanPham;
            }

            Session["flag"] = null;
            Session["keyword"] = null;
            Session["Price1"] = null;
            Session["Price2"] = null;
            GroupProductId = "";
            a = "";
        }
        CollectionPager1.MaxPages = 10000;
        CollectionPager1.PageSize = 10; // số items hiển thị trên một trang     
        current = dlNhomSanPham;
        CollectionPager1.DataSource = dt;
        CollectionPager1.BindToControl = dlNhomSanPham;
        ////dlNhomSanPham.DataSource = CollectionPager1.DataSourcePaged;
        ////dlNhomSanPham.DataBind();
    }

    private void GetGroupProductId(string id)
    {
        if (id == "-1")
        {
            List<Category> dt = new List<Category>();
            dt = CategoryService.Category_GetByAll();
            for (int i = 0; i < dt.Count; i++)
            {
                GroupProductId += dt[i].Id.ToString() + ",";
            }

        }
        else
            GroupProductId = id + ",";
        //DataTable dt = GroupProductService.GroupProduct_GetById(id);
        //DataTable dtChild = GroupProductService.GroupProduct_GetByTop("", "Parent=" + id + " ", "");
        //if (dtChild.Rows.Count > 0)
        //{
        //    for (int i = 0; i < dtChild.Rows.Count; i++)
        //    {
        //        GetGroupProductId(dtChild.Rows[i][0].ToString());
        //    }
        //}
        //else
        //{
        //    GroupProductId += dt.Rows[0][0].ToString() + ",";
        //}
    }
    public string CatChu(string s)
    {
        return FunctionClass.CatChu(s);
    }
    protected string Format_Price(string Price)
    {
        return FunctionClass.Format_Price(Price);        
    }
    protected void dlNhomSanPham_ItemCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
    {
        string strCA = e.CommandArgument.ToString();
        if (e.CommandName == "Choose")
            AddCart(e.CommandArgument.ToString(), "1");
        else
        {
            Ebook dt = EbookService.Ebook_GetById(strCA);
            string Category_Id = dt.Category_Id.ToString();
            string URL = "/Pages/ProductDetail.aspx?Catogory_Id =" + Category_Id + "&Id=" + strCA;
            Response.Redirect(URL);
        }
    }

    protected void AddCart(string productid, string quantity)
    {
        if (Session["Cart"] == null)
            Session["Cart"] = FunctionClass.AddCart(productid, quantity, null);
        else
            Session["Cart"] = FunctionClass.AddCart(productid, quantity, (DataTable)Session["Cart"]);
    }

    protected void dlNhomSanPham_PreRender(object sender, EventArgs e)
    {
        dlNhomSanPham.DataSource = CollectionPager1.DataSourcePaged;
        dlNhomSanPham.DataBind();
    }
}